﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimplePMT.Data;

namespace SimplePMT.Components
{
    public static class Common
    {
        public static DateTime[] GetWeekStartEndDates(string CurrentDate)
        {
            List<string> days = new List<string>();
            DateTime StartDate = new DateTime();
            DateTime EndDate = new DateTime();
            days.Add("Sunday");
            days.Add("Monday");
            days.Add("Tuesday");
            days.Add("Wednesday");
            days.Add("Thursday");
            days.Add("Friday");
            days.Add("Saturday");
            int i = 0;
            DateTime Current = Convert.ToDateTime(CurrentDate);
            foreach (string day in days)
            {

                if (day.Equals(Current.DayOfWeek.ToString()))
                {
                    StartDate = Current.AddDays(-i);
                    EndDate = StartDate.AddDays(6);
                }
                i++;
            }
            return new DateTime[] { StartDate, EndDate };

        }

        public static string[] GetIterationDateRange(string CurrentDate)
        {
            DateTime currentDate = Convert.ToDateTime(CurrentDate);
            if (currentDate.Day >= 1 && currentDate.Day <= 15)
            {
                return new string[] { DateTime.Now.Month +"/1/"+ DateTime.Now.Year, DateTime.Now.Month +"/15/"+ DateTime.Now.Year };
            }
            else
            {
                return new string[] { DateTime.Now.Month + "/16/" + DateTime.Now.Year, Convert.ToDateTime(DateTime.Now.Month +"/1/"+ DateTime.Now.Year).AddMonths(1).AddDays(-1).ToShortDateString()};
            }
        }

        public static string GetRecursiveTaskTreePath(TaskContainer container)
        {
            if (container.ID.Equals(0))
            {
                return "error";
            }
            else
            {
                string tmpPath = container.Label;

                if (container.ParentContainerID > 0)
                {
                    tmpPath = GetRecursiveTaskTreePath(TaskContainer.Find(container.ParentContainerID) ?? new TaskContainer()) + " : " + tmpPath;
                }

                return tmpPath;
            }
        }
        public static string GetBaseTaskTreeLabel(int containerId)
        {
            TaskContainer project = TaskContainer.Find(GetRecursiveTaskTreeID(TaskContainer.Find(containerId)));
            if (project.ID > 0)
            {
                return project.Label;
            }
            else
            {
                return "";
            }
        }

        public static int GetRecursiveTaskTreeID(TaskContainer container)
        {
            if (container.ID.Equals(0))
            {
                return 0;
            }
            else
            {
                int tmpID = container.ID;

                if (container.ParentContainerID > 0)
                {
                    tmpID = GetRecursiveTaskTreeID(TaskContainer.Find(container.ParentContainerID));
                }
                else
                {
                    tmpID = container.ID;
                }

                return tmpID;
            }
        }



        public static string ConvertToHTML(string formText)
        {
            return formText.Replace("\r\n", "<br/>").Replace("\r\n\r\n", "<p/>");
        }
    }
}
